//
//  NSDictionary+JSONCategories.h
//  WebServ1
//
//  Created by Job Search on 27/07/14.
//
//

#ifndef WebServ1_NSDictionary_JSONCategories_h
#define WebServ1_NSDictionary_JSONCategories_h

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:
(NSString*)urlAddress;
-(NSData*)toJSON;
@end

@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:
(NSString*)urlAddress
{
    id result;
    NSData* data = [NSData dataWithContentsOfURL:
                    [NSURL URLWithString: urlAddress] ];
    __autoreleasing NSError* error = nil;
    if(data){
    result = [NSJSONSerialization JSONObjectWithData:data
                                                options:kNilOptions error:&error];
    }
    if (error != nil) return nil;
    return result;
}

-(NSData*)toJSON
{
    NSError* error = nil;
    id result = [NSJSONSerialization dataWithJSONObject:self
                                                options:kNilOptions error:&error];
    if (error != nil) return nil;
    return result;    
}
@end

#endif
